﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_ArticleEdit : System.Web.UI.Page
{
    protected void Bind()
    {
        string str = (string)this.Session["MenuId"];
        string text1 = (string)this.Session["FileName"];
        string str2 = (string)this.Session["MenuName"];
        if (str == null)
        {
            ManagerLogin.Out();
        }
        else
        {
            string str3 = str;
            this.ArticleLink.NavigateUrl = "Article.aspx?menuId=" + str3;
            this.MenuName.Text = str2;
            string str4 = base.Request.QueryString["ArticleId"];
            if (str4 == null)
            {
                this.ShowArticleId.Text = "→ <a href=\"ArticleList.aspx\">请选择需编辑的文章</a>";
                this.Msg.Text = "→ <a href=\"ArticleList.aspx\">请选择需编辑的文章</a>";
                this.Msg.CssClass = "errorMsg";
                this.Msg.Visible = true;
                this.Edit.Enabled = false;
            }
            else
            {
                IDb db = new DbFactory().DriverDb();
                db.Open();
                DataTable oldTable = db.DataTable("ClassName,ParentId,ArticleClassId", "ArticleClass", string.Format("MenuId={0}", str3), "OrderId ASC");
                oldTable.Columns.Add("ClassNameText");
                DataTable newTable = oldTable.Clone();
                Function.PopulateTable(oldTable, newTable, "ClassName", "ArticleClassId", "ParentId", 0, 0);
                this.ArticleClassIdList.DataSource = newTable;
                this.ArticleClassIdList.DataTextField = "ClassNameText";
                this.ArticleClassIdList.DataValueField = "ArticleClassId";
                this.ArticleClassIdList.DataBind();
                DataRow row = db.TopRow("Title,ArticleClassId,ArticleId,ArticleContent,Author,AddTime,weight,keywords,PageDesc", "article", string.Format("articleId={0}", str4), "");
                db.Close();
                this.ShowArticleId.Text = (string)row["ArticleId"];
                this.ArticleTitle.Text = (string)row["Title"];
                this.Author.Text = (string)row["Author"];
                this.ArticleContent.Text = (string)row["ArticleContent"];
                this.AddTime.Text = (string)row["AddTime"];
                this.ArticleClassIdList.SelectedValue = (string)row["ArticleClassId"];
                this.txtorder.Text = row["weight"].ToString();
                this.txtkeys.Text = row["keywords"].ToString();
                this.textDesc.Text = row["PageDesc"].ToString();
                this.Edit.Enabled = true;
                if (!ManagerLogin.ValidateRole(string.Format("1_{0}_a", this.ArticleClassIdList.SelectedValue)))
                {
                    this.Msg.CssClass = "errorMsg";
                    this.Msg.Text = "您无此权限";
                    this.Msg.Visible = true;
                    this.Edit.Enabled = false;
                }
            }
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            Convert.ToDateTime(this.AddTime.Text);
            args.IsValid = true;
        }
        catch
        {
            args.IsValid = false;
        }
    }

    protected void Edit_Click(object sender, EventArgs e)
    {
        if (base.IsValid)
        {
            this.Edit.Enabled = true;
            if (!ManagerLogin.ValidateRole(string.Format("1_{0}_a", this.ArticleClassIdList.SelectedValue)))
            {
                this.Msg.CssClass = "errorMsg";
                this.Msg.Text = "您无此权限";
                this.Msg.Visible = true;
                this.Edit.Enabled = false;
            }
            else
            {
                string[,] includeColumns = new string[,] { 
                    { "Title", this.ArticleTitle.Text.ToString(), "NVarChar", "0" }, 
                    { "Author", this.Author.Text.ToString(), "NVarChar", "32" }, 
                    { "AddTime", this.AddTime.Text.ToString(), "DateTime", "0" }, 
                    { "ArticleClassId", this.ArticleClassIdList.SelectedValue.ToString(), "int", "0" }, 
                    { "ArticleContent", this.ArticleContent.Text.ToString(), "NText", "0" },
                    { "Weight", this.txtorder.Text,"int","32" },
                    { "keywords", txtkeys.Text.Trim(), "NVarChar", "0" },
                    {"PageDesc",textDesc.Text.Trim(),"NVarChar", "0"}
                };

                IDb db = new DbFactory().DriverDb();
                db.Open();
                db.Update("Article", includeColumns, string.Format("ArticleId ={0}", this.ShowArticleId.Text));
                db.Close();
                this.Msg.Text = "文章更新成功！";
                this.Msg.Visible = true;
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
        else
        {
            this.Msg.CssClass = "msg";
        }
    }
}
